package ua.edu.nuos.jeetraining2012.cms.util.google.api.services.drive;

import com.google.api.client.googleapis.media.MediaHttpUploader;
import com.google.api.client.googleapis.media.MediaHttpUploaderProgressListener;
import org.slf4j.Logger;

import java.io.IOException;
import java.text.NumberFormat;

/**
 * FileUploadProgressListener.
 * <p/>
 * Service class that check current upload status.
 *
 * @author Victor N. Ganichev victor.ganichev@gmail.com
 * @version 1.0 Date: 09.06.13 Time: 11:09
 * @since 1.0
 */
public class FileUploadProgressListener implements MediaHttpUploaderProgressListener {

    private Logger logger = org.slf4j.LoggerFactory.getLogger(FileUploadProgressListener.class);

    @Override
    public void progressChanged(MediaHttpUploader uploader) throws IOException {
        switch (uploader.getUploadState()) {
            case INITIATION_STARTED:
                logger.info("Initiation started");
                break;
            case INITIATION_COMPLETE:
                logger.info("Initiation complete.");
                break;
            case MEDIA_IN_PROGRESS:
                logger.info("Uploaded {} percents.", NumberFormat.getPercentInstance().format(uploader.getProgress()));
                break;
            case MEDIA_COMPLETE:
                logger.info("Uploading finished.");
                break;
        }
    }
}
